package InterfaceOffer;

import java.util.Scanner;

class Edge {
    int x;
    int y;
    int u;
    int v;
    int w;

    public Edge(int x, int y, int u, int v, int w) {
        this.x = x;
        this.y = y;
        this.u = u;
        this.v = v;
        this.w = w;
    }
}
// 40分钟，AC了 27% 超出时间限制
public class MtSolution1 {
    public static void main(String[] args) {
        // 第一行输入行数n，列数m，五元组个数k
        Scanner in = new Scanner(System.in);
        int n, m, k;
        n = in.nextInt();
        m = in.nextInt();
        k = in.nextInt();
        Edge[] edges = new Edge[k];
        for (int i = 0; i < k; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            int u = in.nextInt();
            int v = in.nextInt();
            int w = in.nextInt();
            if (u != 1 && v != 1)
                edges[i] = new Edge(x, y, u, v, w);
        }
        /*
         * dp[i][j] 表示坐标截止到(i,j)时，花费的最小总和，初始化无穷大
         * 对于每个坐标，都要遍历一遍edges，找到是否存在(u,v)==(i,j),获得其(x,y,w)
         * dp[i][j] = min( dp[X][Y]+W ),其中，X=x_i,Y=y_i,W=w_i
         * */
        int[][] dp = new int[n + 1][m + 1];
        for (int i = 1; i < n + 1; i++) {
            for (int j = 1; j < m + 1; j++) {
                dp[i][j] = Integer.MAX_VALUE;
            }
        }
        dp[1][1] = 0; // 初始化完毕，到起点的花费为0
        // 开始dp
        for (int i = 1; i < n + 1; i++) {
            for (int j = 2; j < m + 1; j++) {
                // 先遍历edges，找到所有的x和y
                // 每次找的时候，顺便记录当前的min
                int min = Integer.MAX_VALUE;
                for (int p = 0; p < edges.length; p++) {
                    if (edges[p].u == i && edges[p].v == j) {
                        // 防止最大正数+edges[p].w变成正数
                        if (dp[edges[p].x][edges[p].y] != Integer.MAX_VALUE)
                            min = Math.min(min, dp[edges[p].x][edges[p].y] + edges[p].w);
                    }
                }
                dp[i][j] = min;
            }
        }
        if (dp[n][m] == Integer.MAX_VALUE)
            System.out.println(-1);
        else {
            System.out.println(dp[n][m]);
        }
    }
}
